package com.example.javamaday.leetcode;

import com.example.javamaday.leetcode.base.TreeNode;

public class LeetCodeTest0112 {

    public static void main(String[] args) {

    }


    private boolean hasPathSum = false;
    private int targetSum;

    public boolean hasPathSum(TreeNode root, int targetSum) {
        this.targetSum = targetSum;
        if (root == null) {
            return false;
        }
        TreeNode sumRoot = new TreeNode(root.val);
        scanTree(root, sumRoot);
        return hasPathSum;
    }

    private void scanTree(TreeNode root, TreeNode sumRoot) {
        if (root.left == null && root.right == null) {
            if (sumRoot.val == targetSum) {
                hasPathSum = true;
            }
        }
        if(root.left != null) {
            sumRoot.left = new TreeNode(sumRoot.val + root.left.val);
            System.out.println("Left:" + sumRoot.left.val);
            scanTree(root.left, sumRoot.left);
        }
        if(root.right != null) {
            sumRoot.right = new TreeNode(sumRoot.val + root.right.val);
            System.out.println("Right:" + sumRoot.right.val);
            scanTree(root.right, sumRoot.right);
        }
    }
}
